package com.wqnmdb.im.utils;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author 王欣
 *
 */
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class DateUtil {

    /**
     * 获取当前时间戳
     */
	public static int getNowInt() {
		Date nowc = new Date();
		return (int)(nowc.getTime() / 1000);
	}

    /**
     * 秒转化为时间格式  yyyy-MM-dd HH:mm:ss
     */
    public static String dateToString(int second, String sFormat) {
        Date dDate = new Date((long) second * 1000);
        if (dDate == null) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
            return sdf.format(dDate);
        }
    }

    /**
     * 获取当前string时间
     * @return
     */
    public static String getNow() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowc = new Date();
        String pid = formatter.format(nowc);
        return pid;
    }

	/**
	 * 获取取当前日期 "yyyy-MM-dd H:mm:ss"
	 */
	public static String getNowDate(String sFormat) {
		SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
		Date nowc = new Date();
		String pid = formatter.format(nowc);
		return pid;
	}


	/**
	 * 将日期字符串参数转为 int
	 */
	public static int dateToInt(String fromdate){
		try{
			if(fromdate.length() == 12 || fromdate.length() == 13){
				return (int)(new SimpleDateFormat("yyyy-MM-dd HH").parse(fromdate).getTime()/1000);
			}else if(fromdate.length()>10){
				return (int)(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fromdate).getTime()/1000);
			}else{
				return (int)(new SimpleDateFormat("yyyy-MM-dd").parse(fromdate).getTime()/1000);
			}
		}catch(Exception ex){
			return 0;
		}
	}
}
